using System;
using System.Collections.Generic;
using System.Linq;
using Firestarter.DB.Schema.BO;
using Firestarter.DB.Schema.Test.BO.TestUtils;
using Firestarter.DB_DomainModel.Logic.Test.DBChangesImporterTests;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.Test;
using FireStarterModeller.Test.BO;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.Smooth;
using Habanero.Testability;
using NUnit.Framework;
using Rhino.Mocks;

namespace Firestarter.DB_DomainModel.Logic.Test
{
    [TestFixture]
    public class TestDBToDMRelationshipImporter : TestBase
    {
        [SetUp]
        public override void SetupTest()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [Test]
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            IRelationshipNameGenerator relationshipNameGenerator =
                MockRepository.GenerateStub<IRelationshipNameGenerator>();
            var defClassFactory = new DefClassFactory();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DBToDMRelationshipImporter dbToDMRelationshipImporter =
                new DBToDMRelationshipImporter(relationshipNameGenerator, defClassFactory);
            //---------------Test Result -----------------------
            Assert.IsNotNull(dbToDMRelationshipImporter);
            Assert.AreSame(relationshipNameGenerator, dbToDMRelationshipImporter.RelationshipNameGenerator);
        }

        [Test]
        public void Test_Construct_WhenNullRelationshipNameGenerator_ShouldThrowError()
        {
            //---------------Set up test pack-------------------
            var defClassFactory = new DefClassFactory();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
                new DBToDMRelationshipImporter(null, defClassFactory);
                Assert.Fail("expected ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("relationshipNameGenerator", ex.ParamName);
            }
        }

        [Test]
        public void Test_Import_WhenDBForeignKeyIsNull_ShouldThrowError()
        {
            //---------------Set up test pack-------------------
            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
                IList<string> errorMessages;
                dbToDMRelationshipImporter.Import(null, out errorMessages);
                Assert.Fail("expected ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("dbForeignKey", ex.ParamName);
            }
        }

        [Test]
        public void Test_GetMappedRelationship_WhenNoneMapped_ShouldCreateOne()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = new DBForeignKey();
            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();
            //---------------Assert Precondition----------------
            Assert.IsNull(dbForeignKey.GetMappedDMRelationship());
            //---------------Execute Test ----------------------
            DMRelationship dmRelationship = dbToDMRelationshipImporter.GetMappedRelationship(dbForeignKey);
            //---------------Test Result -----------------------
            Assert.AreEqual(Cardinality.Single, dmRelationship.Cardinality);
            Assert.IsNull(dmRelationship.ReverseRelationship);
            Assert.AreSame(dbForeignKey, dmRelationship.MappedDBForeignKey);
            Assert.AreEqual(DeleteParentAction.DoNothing, dmRelationship.DeleteAction);
        }

        [Test]
        public void Test_GetMappedRelationship_WhenNoneMapped_ShouldCreateOne_ShouldNotMapDBForeignKey()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = new DBForeignKey();
            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();
            //---------------Assert Precondition----------------
            Assert.IsNull(dbForeignKey.GetMappedDMRelationship());
            //---------------Execute Test ----------------------
            DMRelationship dmRelationship = dbToDMRelationshipImporter.GetMappedRelationship(dbForeignKey);
            //---------------Test Result -----------------------
            Assert.AreEqual(Cardinality.Single, dmRelationship.Cardinality);
            Assert.IsNull(dmRelationship.ReverseRelationship);
            Assert.AreSame(dbForeignKey, dmRelationship.MappedDBForeignKey);
            Assert.AreEqual(DeleteParentAction.DoNothing, dmRelationship.DeleteAction);
        }

        [Test]
        public void Test_GetMappedRelationship_WhenMapped_ShouldReturnMappedRel_Unchanged()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = new DBForeignKey();
            var expectedRelationship = new DMRelationship();
            expectedRelationship.Cardinality = Cardinality.Multiple;
            dbForeignKey.SetMappedDMRelationship(expectedRelationship);
            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dbForeignKey.GetMappedDMRelationship());
            Assert.AreEqual(DeleteParentAction.Prevent, expectedRelationship.DeleteAction);
            Assert.IsNotNull(expectedRelationship.ReverseRelationship);
            //---------------Execute Test ----------------------
            DMRelationship dmRelationship = dbToDMRelationshipImporter.GetMappedRelationship(dbForeignKey);
            //---------------Test Result -----------------------
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.Cardinality);
            Assert.IsNotNull(dmRelationship.ReverseRelationship);
            Assert.AreSame(dbForeignKey, dmRelationship.MappedDBForeignKey);
            Assert.AreEqual(DeleteParentAction.Prevent, dmRelationship.DeleteAction);
            Assert.AreSame(expectedRelationship, dmRelationship);
        }

        [Test]
        public void Test_GetMappedRelationship_ShouldReturnMappedRel_WithParentDMClassSet()
        {
            //---------------Set up test pack-------------------
            var dbForeignKey = new DBForeignKey {ParentTable = new DBTable(), RelatedTable = new DBTable()};
            var parentClass = new DMClass();
            dbForeignKey.ParentTable.SetMappedDMClass(parentClass);

            DMClass relatedClass = new DMClass
                                       {Assembly = new DMAssembly(GetRandomString()), ClassNameBO = GetRandomString()};

            dbForeignKey.RelatedTable.SetMappedDMClass(relatedClass);
                
            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBToDMRelationshipImporterWithFakeDefFactory();
            //---------------Assert Precondition----------------
            Assert.IsNull(dbForeignKey.GetMappedDMRelationship());
            Assert.IsNotNull(dbForeignKey.ParentDMClass());
            Assert.IsNotNull(dbForeignKey.RelatedDMClass());
            Assert.IsNotNullOrEmpty(relatedClass.AssemblyName);
            Assert.IsNotNullOrEmpty(relatedClass.ClassNameBO);
            //---------------Execute Test ----------------------
            DMRelationship dmRelationship = dbToDMRelationshipImporter.GetMappedRelationship(dbForeignKey);
            //---------------Test Result -----------------------
            Assert.AreSame(parentClass, dmRelationship.OwnerClass);
            Assert.AreEqual(relatedClass.ClassNameBO, dmRelationship.RelatedClassName);
            Assert.AreEqual(relatedClass.AssemblyName, dmRelationship.RelatedAssemblyName);
        }

        private DBToDMRelationshipImporter GetDBToDMRelationshipImporterWithFakeDefFactory()
        {
            IRelationshipNameGenerator relationshipNameGenerator =
                MockRepository.GenerateStub<IRelationshipNameGenerator>();
            IDefClassFactory classFactory = new FakeClassFactory();

            return new DBToDMRelationshipImporter(relationshipNameGenerator, classFactory);
        }

        private static void SetRelatedClass(FakeDBForeignKey dbForeignKey, DMClass relatedClass)
        {
            dbForeignKey.Stub(key => key.RelatedDMClass()).Return(relatedClass);
        }

        private static void SetParentClass(FakeDBForeignKey dbForeignKey, DMClass parentClass)
        {
            dbForeignKey.Stub(key => key.ParentDMClass()).Return(parentClass);
        }
/*
        [Test]
        public void Test_CreateRelationshipProperties_WhenNon_ShouldDoNothing()
        {
            //---------------Set up test pack-------------------
            var relationship = new DMRelationship(null);
            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();
            //---------------Assert Precondition----------------
            Assert.IsEmpty(relationship.RelationshipProperties);
            //---------------Execute Test ----------------------
            dbToDMRelationshipImporter.CreateRelationshipProperties(relationship, new DBForeignKeyColumn[0]);
            //---------------Test Result -----------------------
            Assert.IsEmpty(relationship.RelationshipProperties);
        }

        [Test]
        public void Test_CreateRelationshipProperties_WhenhasOne_ShouldCreateRelProp()
        {
            //---------------Set up test pack-------------------
            var relationship = new DMRelationship(null);
            var fkColumn = GetDBForeignKeyColumn();

            var foreignKeyColumnInfos = new[] {fkColumn};
            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();
            //---------------Assert Precondition----------------
            Assert.IsEmpty(relationship.RelationshipProperties);
            Assert.IsNotNullOrEmpty(fkColumn.GetParentPropertyName());
            Assert.IsNotNullOrEmpty(fkColumn.GetRelatedPropertyName());
            //---------------Execute Test ----------------------
            dbToDMRelationshipImporter.CreateRelationshipProperties(relationship, foreignKeyColumnInfos);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, relationship.RelationshipProperties.Count);
            AssertRelPropCreated(fkColumn, relationship);
        }

        [Test]
        public void Test_CreateRelationshipProperties_WhenhasTwo_ShouldCreateRelProp()
        {
            //---------------Set up test pack-------------------
            var relationship = new DMRelationship(null);
            var fkColumn = GetDBForeignKeyColumn();
            var fkColumn2 = GetDBForeignKeyColumn();
            var foreignKeyColumnInfos = new[] {fkColumn, fkColumn2};
            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();
            //---------------Assert Precondition----------------
            Assert.IsEmpty(relationship.RelationshipProperties);
            Assert.IsNotNullOrEmpty(fkColumn.GetParentPropertyName());
            Assert.IsNotNullOrEmpty(fkColumn.GetRelatedPropertyName());
            //---------------Execute Test ----------------------
            dbToDMRelationshipImporter.CreateRelationshipProperties(relationship, foreignKeyColumnInfos);
            //---------------Test Result -----------------------
            Assert.AreEqual(2, relationship.RelationshipProperties.Count);
            AssertRelPropCreated(fkColumn, relationship);
            AssertRelPropCreated(fkColumn2, relationship);
        }*/
/*
        [Test]
        public void Test_FindDBFKColumn_WhenOneMatches_ShouldReturnMatch()
        {
            //---------------Set up test pack-------------------
            DBForeignKeyColumn fkColumn = GetDBForeignKeyColumn();
            DBForeignKeyColumn fkColumn2 = GetDBForeignKeyColumn();
            var foreignKeyColumnInfos = new[] {fkColumn, fkColumn2};
            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();
            DMRelationshipProperty relationshipProperty = dbToDMRelationshipImporter.CreateRelationshipProperty(fkColumn);

            //---------------Assert Precondition----------------
            Assert.AreEqual(fkColumn.GetParentPropertyName(), relationshipProperty.OwnerPropertyName);
            Assert.AreEqual(fkColumn.GetRelatedPropertyName(), relationshipProperty.RelatedPropertyName);
            //---------------Execute Test ----------------------
            DBForeignKeyColumn foundFKCol = dbToDMRelationshipImporter.FindForeignKeyColumn(foreignKeyColumnInfos,
                                                                                            relationshipProperty);
            //---------------Test Result -----------------------
            Assert.AreSame(fkColumn, foundFKCol);
        }*/

        [Test]
        public void Test_FindDBFKColumn_WhenMatchesParentPropOnly_ShouldReturnNull()
        {
            //---------------Set up test pack-------------------
            DBForeignKeyColumn fkColumn = GetDBForeignKeyColumn();
            DBForeignKeyColumn fkColumn2 = GetDBForeignKeyColumn();
            var foreignKeyColumnInfos = new[] {fkColumn, fkColumn2};

            var parentPropertyName = fkColumn.GetParentPropertyName();
            DMRelationshipProperty relationshipProperty = new DMRelationshipProperty(null)
                                                              {
                                                                  OwnerPropertyName = parentPropertyName,
                                                                  RelatedPropertyName = GetRandomString()
                                                              };

            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();

            //---------------Assert Precondition----------------
            Assert.AreEqual(fkColumn.GetParentPropertyName(), relationshipProperty.OwnerPropertyName);
            Assert.AreNotEqual(fkColumn.GetRelatedPropertyName(), relationshipProperty.RelatedPropertyName);
            //---------------Execute Test ----------------------
            DBForeignKeyColumn foundFKCol = dbToDMRelationshipImporter.FindForeignKeyColumn(foreignKeyColumnInfos,
                                                                                            relationshipProperty);
            //---------------Test Result -----------------------
            Assert.IsNull(foundFKCol);
        }

        [Test]
        public void Test_FindDBFKColumn_WhenMatchesRelatedPropOnly_ShouldReturnNull()
        {
            //---------------Set up test pack-------------------
            DBForeignKeyColumn fkColumn = GetDBForeignKeyColumn();
            DBForeignKeyColumn fkColumn2 = GetDBForeignKeyColumn();
            var foreignKeyColumnInfos = new[] {fkColumn, fkColumn2};
            var relatedPropName = fkColumn.GetRelatedPropertyName();
            DMRelationshipProperty relationshipProperty = new DMRelationshipProperty(null)
                                                              {
                                                                  OwnerPropertyName = GetRandomString(),
                                                                  RelatedPropertyName = relatedPropName
                                                              };

            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();
            //---------------Assert Precondition----------------
            Assert.AreNotEqual(fkColumn.GetParentPropertyName(), relationshipProperty.OwnerPropertyName);
            Assert.AreEqual(fkColumn.GetRelatedPropertyName(), relationshipProperty.RelatedPropertyName);
            //---------------Execute Test ----------------------
            DBForeignKeyColumn foundFKCol = dbToDMRelationshipImporter.FindForeignKeyColumn(foreignKeyColumnInfos,
                                                                                            relationshipProperty);
            //---------------Test Result -----------------------
            Assert.IsNull(foundFKCol);
        }

        [Test]
        public void Test_DeleteRelProps_WhenNoFKColumn_ShouldMarkForDelete()
        {
            //---------------Set up test pack-------------------
            DBForeignKeyColumn fkColumn = GetDBForeignKeyColumn();
            DBForeignKeyColumn fkColumn2 = GetDBForeignKeyColumn();
            var foreignKeyColumnInfos = new[] {fkColumn, fkColumn2};
            var relatedPropName = fkColumn.GetRelatedPropertyName();
            IDMRelationshipProperty relationshipProperty = MockRepository.GenerateStub<IDMRelationshipProperty>();
            relationshipProperty.OwnerPropertyName = GetRandomString();
            relationshipProperty.RelatedPropertyName = relatedPropName;

            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();
            //---------------Assert Precondition----------------
            Assert.AreNotEqual(fkColumn.GetParentPropertyName(), relationshipProperty.OwnerPropertyName);
            Assert.AreEqual(fkColumn.GetRelatedPropertyName(), relationshipProperty.RelatedPropertyName);
            relationshipProperty.AssertWasNotCalled(property => property.MarkForDelete());
            //---------------Execute Test ----------------------
            dbToDMRelationshipImporter.DeleteAllRelPropsWithNoFKColumn(new[] {relationshipProperty},
                                                                       foreignKeyColumnInfos);
            //---------------Test Result -----------------------
            relationshipProperty.AssertWasCalled(property => property.MarkForDelete());
        }

        [Test]
        public void Test_DeleteRelProps_WhenHasFKColumn_ShouldNotMarkForDelete()
        {
            //---------------Set up test pack-------------------
            DBForeignKeyColumn fkColumn = GetDBForeignKeyColumn();
            DBForeignKeyColumn fkColumn2 = GetDBForeignKeyColumn();
            var foreignKeyColumnInfos = new[] {fkColumn, fkColumn2};
            var relatedPropName = fkColumn.GetRelatedPropertyName();
            IDMRelationshipProperty relationshipProperty = MockRepository.GenerateStub<IDMRelationshipProperty>();
            relationshipProperty.OwnerPropertyName = fkColumn.GetParentPropertyName();
            relationshipProperty.RelatedPropertyName = relatedPropName;

            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();
            //---------------Assert Precondition----------------
            Assert.AreEqual(fkColumn.GetParentPropertyName(), relationshipProperty.OwnerPropertyName);
            Assert.AreEqual(fkColumn.GetRelatedPropertyName(), relationshipProperty.RelatedPropertyName);
            relationshipProperty.AssertWasNotCalled(property => property.MarkForDelete());
            //---------------Execute Test ----------------------
            dbToDMRelationshipImporter.DeleteAllRelPropsWithNoFKColumn(new[] {relationshipProperty},
                                                                       foreignKeyColumnInfos);
            //---------------Test Result -----------------------
            relationshipProperty.AssertWasNotCalled(property => property.MarkForDelete());
        }

        [Test]
        public void Test_DeleteRelProps_WhenOneHasFKAndOnNot_ShouldMarkOneAsDeleteAndOneNot()
        {
            //---------------Set up test pack-------------------
            DBForeignKeyColumn fkColumn = GetDBForeignKeyColumn();
            DBForeignKeyColumn fkColumn2 = GetDBForeignKeyColumn();
            var foreignKeyColumnInfos = new[] {fkColumn, fkColumn2};
            var relatedPropName = fkColumn.GetRelatedPropertyName();

            IDMRelationshipProperty relPropMatches = MockRepository.GenerateStub<IDMRelationshipProperty>();
            relPropMatches.OwnerPropertyName = fkColumn.GetParentPropertyName();
            relPropMatches.RelatedPropertyName = relatedPropName;

            IDMRelationshipProperty relPropNotMatches = MockRepository.GenerateStub<IDMRelationshipProperty>();
            relPropNotMatches.OwnerPropertyName = GetRandomString();
            relPropNotMatches.RelatedPropertyName = relatedPropName;

            DBToDMRelationshipImporter dbToDMRelationshipImporter = GetDBtoDMRelationshipImporter();
            //---------------Assert Precondition----------------
            Assert.AreNotEqual(fkColumn.GetParentPropertyName(), relPropNotMatches.OwnerPropertyName);
            Assert.AreEqual(fkColumn.GetRelatedPropertyName(), relPropNotMatches.RelatedPropertyName);
            relPropNotMatches.AssertWasNotCalled(property => property.MarkForDelete());
            relPropMatches.AssertWasNotCalled(property => property.MarkForDelete());
            //---------------Execute Test ----------------------
            dbToDMRelationshipImporter.DeleteAllRelPropsWithNoFKColumn(new[] {relPropNotMatches}, foreignKeyColumnInfos);
            //---------------Test Result -----------------------
            relPropNotMatches.AssertWasCalled(property => property.MarkForDelete());
            relPropMatches.AssertWasNotCalled(property => property.MarkForDelete());
        }

        //This test is here since this method is going to be moved to use 
        // extention methods.
        [Test]
        public void Test_ParentDMClass_WhenHasOneShouldReturnClass()
        {
            //---------------Set up test pack-------------------
            DBForeignKey foreignKey = new DBForeignKey();
            foreignKey.ParentTable = new DBTable("");
            var expectedClass = new DMClass();
            foreignKey.ParentTable.SetMappedDMClass (expectedClass);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(foreignKey.ParentTable.GetMappedDMClass());
            //---------------Execute Test ----------------------
            var parentDMClass = foreignKey.ParentDMClass();
            //---------------Test Result -----------------------
            Assert.AreSame(expectedClass, parentDMClass);
        }

        [Test]
        public void Test_ParentDMClass_WhenNotHasOneShouldReturnNull()
        {
            //---------------Set up test pack-------------------
            DBForeignKey foreignKey = new DBForeignKey {ParentTable = new DBTable("")};
            //---------------Assert Precondition----------------
            Assert.IsNull(foreignKey.ParentTable.GetMappedDMClass());
            //---------------Execute Test ----------------------
            var parentDMClass = foreignKey.ParentDMClass();
            //---------------Test Result -----------------------
            Assert.IsNull(parentDMClass);
        }

        [Test]
        public void Test_ParentDMClass_WhenNotHasDBTableShouldReturnNull()
        {
            //---------------Set up test pack-------------------
            DBForeignKey foreignKey = new DBForeignKey();
            //---------------Assert Precondition----------------
            Assert.IsNull(foreignKey.ParentTable);
            //---------------Execute Test ----------------------
            var parentDMClass = foreignKey.ParentDMClass();
            //---------------Test Result -----------------------
            Assert.IsNull(parentDMClass);
        }

        //This test is here since this method is going to be moved to use 
        // extention methods.
        [Test]
        public void Test_RelatedDMClass_WhenHasOneShouldReturnClass()
        {
            //---------------Set up test pack-------------------
            DBForeignKey foreignKey = new DBForeignKey();
            foreignKey.RelatedTable = new DBTable("");
            var expectedClass = new DMClass();
            foreignKey.RelatedTable.SetMappedDMClass (expectedClass);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(foreignKey.RelatedTable.GetMappedDMClass());
            //---------------Execute Test ----------------------
            var relatedDMClass = foreignKey.RelatedDMClass();
            //---------------Test Result -----------------------
            Assert.AreSame(expectedClass, relatedDMClass);
        }

        [Test]
        public void Test_RelatedDMClass_WhenNotHasOneShouldReturnNull()
        {
            //---------------Set up test pack-------------------
            DBForeignKey foreignKey = new DBForeignKey {RelatedTable = new DBTable("")};
            //---------------Assert Precondition----------------
            Assert.IsNull(foreignKey.RelatedTable.GetMappedDMClass());
            //---------------Execute Test ----------------------
            var relatedDMClass = foreignKey.RelatedDMClass();
            //---------------Test Result -----------------------
            Assert.IsNull(relatedDMClass);
        }

        [Test]
        public void Test_RelatedDMClass_WhenNotHasDBTableShouldReturnNull()
        {
            //---------------Set up test pack-------------------
            DBForeignKey foreignKey = new DBForeignKey();
            //---------------Assert Precondition----------------
            Assert.IsNull(foreignKey.RelatedTable);
            //---------------Execute Test ----------------------
            var relatedDMClass = foreignKey.RelatedDMClass();
            //---------------Test Result -----------------------
            Assert.IsNull(relatedDMClass);
        }

        [Test]
        public void Test_Import_WithNoExistingRelationship_ShouldCreateAsManyToOne()
        {
            //---------------Set up test pack-------------------
            DBColumn dbColumn = CreateColumnWithMappedProp("Some Table", "Some Column");
            DBColumn relatedDBColumn = CreateColumnWithMappedProp("Related Table", "relatedDBColumn");

            DBForeignKey dbForeignKey = new DBForeignKey {ForeignKeyName = "ForeignKeyName"};
            SetValidOwnerAndRelatedTable(dbForeignKey);

            dbForeignKey.AddColumnInfo(dbColumn, relatedDBColumn);

            IRelationshipNameGenerator relationshipNameGenerator =
                MockRepository.GenerateStub<IRelationshipNameGenerator>();
            relationshipNameGenerator.Stub(t => t.CreateRelationshipName(null)).IgnoreArguments().Return(
                TestUtilsShared.GetRandomString());
            IDMSolution solution = new DMSolution();
            IDefClassFactory classFactory = new FMDefClassFactory(solution);
            DBToDMRelationshipImporter dbToDMRelationshipImporter =
                new DBToDMRelationshipImporter(relationshipNameGenerator, classFactory);
            //---------------Assert Precondition----------------
            Assert.IsNull(dbForeignKey.GetMappedDMRelationship());
            Assert.IsNotNull(dbForeignKey.ParentDMClass());
            Assert.IsNotNull(dbForeignKey.RelatedDMClass());
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship dmRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.AreEqual(Cardinality.Single, dmRelationship.Cardinality);
            Assert.IsNotNull(dmRelationship.ReverseRelationship);
            Assert.AreEqual(Cardinality.Multiple, dmRelationship.ReverseRelationship.Cardinality);
        }

        private void SetValidOwnerAndRelatedTable(DBForeignKey dbForeignKey)
        {
            DBTable dbTable = new DBTable(TestUtilsShared.GetRandomString());
            dbTable.SetMappedDMClass(new DMClass());

            dbForeignKey.ParentTable = dbTable;
            dbForeignKey.RelatedTable = dbTable;
        }


        [Test]
        public void Test_Import_WithExistingRelationship_WhenNoColumns_ShouldCauseErrorLog_ButStillCreateRel()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = CreateForeignKeyWithMappedRelationshipWithOneRelatedProp();
            DMRelationship dmRelationship = dbForeignKey.GetMappedDMRelationship();
//            IDMClass ownerClass = dmRelationship.OwnerClass;

            dbForeignKey.ForeignKeyColumns[0].MarkForDelete();

            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dbForeignKey.ForeignKeyColumns.Count);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship returnedDMRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------

            Assert.AreSame(dmRelationship, returnedDMRelationship);
            Assert.AreEqual(1, errorMessages.Count);
            string message = errorMessages[0];
            string expectedMessage = string.Format
                ("A relationship could not be created for the foreign key " +
                 "called '{0}' on table '{1}' since it does not have any columns",
                 dbForeignKey.ForeignKeyName, dbForeignKey.ParentTable);
            StringAssert.Contains(expectedMessage, message);

            //Assert.AreEqual(0, dbToDMRelationshipImporter.ErrorMessages.Count);
            //Assert.IsNotNull(returnedDMRelationship);
            //Assert.AreSame(dmRelationship, returnedDMRelationship);
            //Assert.AreEqual(0, dmRelationship.RelationshipProperties.Count);
            //Assert.AreSame(returnedDMRelationship.MappedDBForeignKey, dbForeignKey);
        }

        [Test]
        public void Test_Import_WithExistingRelationship_WhenChangedParentColumn_ShouldUpdateRelationshipProperty()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = CreateForeignKeyWithMappedRelationshipWithOneRelatedProp();
            DMRelationship dmRelationship = dbForeignKey.GetMappedDMRelationship();
            DMClass ownerClass = (DMClass) dmRelationship.OwnerClass;

            DBColumn newParentColumn = new DBColumn("NewFKColumn");
            newParentColumn.Table = dbForeignKey.ParentTable;
            DMProperty newParentProperty = new DMProperty {PropertyName = "NewFKProperty"};
            newParentProperty.MappedDBColumn = newParentColumn;
            newParentProperty.Class = ownerClass;

            dbForeignKey.ForeignKeyColumns[0].ParentColumn = newParentColumn;

            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationship);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            Assert.IsNotNull(dmRelationship.RelatedClass);
            Assert.AreNotSame(newParentProperty, dmRelationship.RelationshipProperties[0].OwnerProperty);
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship returnedRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.AreSame(dmRelationship, returnedRelationship);
            Assert.AreSame(dmRelationship, ownerClass.OwnerRelationships[0]);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            Assert.AreSame(newParentProperty.PropertyName, dmRelationship.RelationshipProperties[0].OwnerPropertyName);
//
//            Assert.IsTrue(dmRelationship.Status.IsValid(), "Relationship is not valid." +
//                                                           Environment.NewLine + "Reason: " + dmRelationship.Status.IsValidMessage);
        }


        [Test]
        public void Test_Import_WithExistingRelationship_WhenChangedRelatedColumn_ShouldUpdateRelationshipProperty()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = CreateForeignKeyWithMappedRelationshipWithOneRelatedProp();
            DMRelationship dmRelationship = dbForeignKey.GetMappedDMRelationship();
            DMClass ownerClass = (DMClass) dmRelationship.OwnerClass;
            DMClass relatedClass = (DMClass) dmRelationship.RelatedClass;

            DBColumn newRelatedColumn = new DBColumn("NewRelatedColumn");
            newRelatedColumn.Table = dbForeignKey.RelatedTable;
            DMProperty newRelatedProperty = new DMProperty {PropertyName = "NewRelatedProperty"};
            newRelatedProperty.MappedDBColumn = newRelatedColumn;
            newRelatedProperty.Class = relatedClass;

            dbForeignKey.ForeignKeyColumns[0].RelatedColumn = newRelatedColumn;

            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationship);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            Assert.IsNotNull(dmRelationship.RelatedClass);
            Assert.AreNotSame(newRelatedProperty, dmRelationship.RelationshipProperties[0].RelatedProperty);
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship returnedRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.AreSame(dmRelationship, returnedRelationship);
            Assert.AreSame(dmRelationship, ownerClass.OwnerRelationships[0]);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            Assert.AreSame(newRelatedProperty.PropertyName, dmRelationship.RelationshipProperties[0].RelatedPropertyName);
//
//            Assert.IsTrue(dmRelationship.Status.IsValid(), "Relationship is not valid." +
//                                                           Environment.NewLine + "Reason: " + dmRelationship.Status.IsValidMessage);
        }


        [Test]
        public void
            Test_Import_WithExistingRelationship_WhenChangedBothRelatedAndParentColumns_ShouldUpdateRelationshipProperty
            ()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = CreateForeignKeyWithMappedRelationshipWithOneRelatedProp();
            DMRelationship dmRelationship = dbForeignKey.GetMappedDMRelationship();
            DMClass ownerClass = (DMClass) dmRelationship.OwnerClass;
            DMClass relatedClass = (DMClass) dmRelationship.RelatedClass;

            DBColumn newRelatedColumn = new DBColumn("NewRelatedColumn");
            newRelatedColumn.Table = dbForeignKey.RelatedTable;
            DMProperty newRelatedProperty = new DMProperty {PropertyName = "NewRelatedProperty"};
            newRelatedProperty.MappedDBColumn = newRelatedColumn;
            newRelatedProperty.Class = relatedClass;

            DBColumn newParentColumn = new DBColumn("NewFKColumn");
            newParentColumn.Table = dbForeignKey.ParentTable;
            DMProperty newParentProperty = new DMProperty {PropertyName = "NewFKProperty"};
            newParentProperty.MappedDBColumn = newParentColumn;
            newParentProperty.Class = ownerClass;

            dbForeignKey.ForeignKeyColumns[0].ParentColumn = newParentColumn;
            dbForeignKey.ForeignKeyColumns[0].RelatedColumn = newRelatedColumn;

            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmRelationship);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            Assert.IsNotNull(dmRelationship.RelatedClass);
            Assert.AreNotSame(newRelatedProperty, dmRelationship.RelationshipProperties[0].RelatedProperty);
            Assert.AreNotSame(newParentProperty, dmRelationship.RelationshipProperties[0].OwnerProperty);
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship returnedRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.AreSame(dmRelationship, returnedRelationship);
            Assert.AreSame(dmRelationship, ownerClass.OwnerRelationships[0]);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            Assert.AreSame(newRelatedProperty.PropertyName, dmRelationship.RelationshipProperties[0].RelatedPropertyName);
            Assert.AreSame(newParentProperty.PropertyName, dmRelationship.RelationshipProperties[0].OwnerPropertyName);

/*            Assert.IsTrue(dmRelationship.Status.IsValid(), "Relationship is not valid." +
                                                           Environment.NewLine + "Reason: " + dmRelationship.Status.IsValidMessage);*/
        }

        [Test]
        public void Test_Import_WithNoExistingRelationship_NoColumns_ShouldCreateRelationship_AndRetErrors()
        {
            //---------------Set up test pack-------------------
            DBColumn dbColumn = CreateColumnWithMappedProp("Some Table", "Some Column");
            IDMClass dmClass = dbColumn.GetMappedDMProperty().Class;
            DBTable dbTable = dmClass.MappedTable as DBTable;
            DBTable relatedTable = new DBTable();
            relatedTable.SetMappedDMClass ( new DMClass());
            DBForeignKey dbForeignKey = new DBForeignKey
                                            {
                                                ForeignKeyName = "ForeignKeyName",
                                                ParentTable = dbTable,
                                                RelatedTable = relatedTable
                                            };

            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dbForeignKey.ForeignKeyColumns.Count);
            Assert.AreSame(dbTable, dmClass.MappedTable);
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship dmRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.IsNotNull(dmRelationship);
            Assert.IsNotNullOrEmpty(dmRelationship.RelationshipName);
            Assert.IsEmpty(dmRelationship.RelationshipProperties);
            Assert.AreEqual(1, errorMessages.Count);
            string message = errorMessages[0];
            string expectedMessage = string.Format
                ("A relationship could not be created for the foreign key called '{0}' on table '{1}' since it does not have any columns",
                 dbForeignKey.ForeignKeyName, dbForeignKey.ParentTable);
            StringAssert.Contains(expectedMessage, message);
        }

        [Test]
        public void Test_Import_WithNoExistingRelationship_OneColumns()
        {
            //---------------Set up test pack-------------------
            DBColumn dbColumn = CreateColumnWithMappedProp("Some Table", "Some Column");
            DMClass dmClass = (DMClass) dbColumn.GetMappedDMProperty().Class;
            DBTable dbTable = (DBTable) dmClass.MappedTable;

            DBColumn relatedDBColumn = CreateColumnWithMappedProp("Related Table", "relatedDBColumn");
            IDMClass relatedClass = relatedDBColumn.GetMappedDMProperty().Class;

            DBForeignKey dbForeignKey = new DBForeignKey
                                            {
                                                ForeignKeyName = "ForeignKeyName",
                                                ParentTable = dbTable,
                                                RelatedTable = (DBTable)relatedDBColumn.Table
                                            };
            dbForeignKey.AddColumnInfo(dbColumn, relatedDBColumn);
            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dbForeignKey.ForeignKeyColumns.Count);
            AssertBusinessObjectContainsPropertyForColumn(dmClass, dbColumn);
            Assert.AreEqual(dmClass.MappedTableID, dbTable.TableID);
            Assert.IsNotNull(dmClass.MappedTable, "The class should be mapped to a table");
            Assert.AreSame(dbTable, dmClass.MappedTable);
            Assert.AreSame(dbColumn.ColumnName, dmClass.Properties[0].MappedDBColumnName);
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship dmRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            GetDMSolutionPostLoadProcessor().LinkRelPropToProperties(dmRelationship, dmClass);
            //---------------Test Result -----------------------
            Assert.IsNotNull(dmRelationship);
            Assert.AreSame(dmClass, dmRelationship.OwnerClass);
            Assert.IsNotNull(dmRelationship.MappedDBForeignKey);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            DMProperty expectedBoProp = dmClass.Properties[0];
            DMRelationshipProperty relPropn = dmRelationship.RelationshipProperties[0];
            IDMProperty actualBoProp = relPropn.OwnerProperty;
            Assert.IsNotNull(actualBoProp);
            Assert.AreSame(expectedBoProp, actualBoProp);

//            Assert.IsNotNull(dmRelationship.RelatedClass);
            Assert.AreSame(relatedClass.ClassNameBO, dmRelationship.RelatedClassName);
            Assert.AreSame(relatedClass.AssemblyName, dmRelationship.RelatedAssemblyName);
            Assert.AreSame(relatedClass.Properties[0].PropertyName, relPropn.RelatedPropertyName);

/*
            Assert.IsTrue(dmRelationship.Status.IsValid(), "Relationship is not valid." +
                                                           Environment.NewLine + "Reason: " + dmRelationship.Status.IsValidMessage);
            Assert.IsTrue(dmRelationship.ReverseRelationship.Status.IsValid(), "Reverse Relationship is not valid." +
                                                                               Environment.NewLine + "Reason: " + dmRelationship.ReverseRelationship.Status.IsValidMessage);
*/
        }

        private static DMSolutionPostLoadProcessor GetDMSolutionPostLoadProcessor()
        {
            return new DMSolutionPostLoadProcessor(new AutoResolvingUnityContainer());
        }

        [Test]
        public void Test_Import_WithNoExistingRelationship_OneColumns_NoRelatedClass_ShouldRetNull()
        {
            //---------------Set up test pack-------------------
            DBColumn dbColumn = CreateColumnWithMappedProp("Some Table", "Some Column");
            DMClass dmClass = (DMClass) dbColumn.GetMappedDMProperty().Class;
            DBTable dbTable = dmClass.MappedTable as DBTable;

            DBTable relatedTable = new DBTable("Related Table");
            DBColumn relatedDBColumn = new DBColumn("relatedDBColumn");
            relatedTable.Columns.Add(relatedDBColumn);

            DBForeignKey dbForeignKey = new DBForeignKey
                                            {
                                                ForeignKeyName = "ForeignKeyName",
                                                ParentTable = dbTable,
                                                RelatedTable = relatedTable
                                            };
            dbForeignKey.AddColumnInfo(dbColumn, relatedDBColumn);
            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dbForeignKey.ForeignKeyColumns.Count);
            AssertBusinessObjectContainsPropertyForColumn(dmClass, dbColumn);
            Assert.IsNotNull(dbTable);
            Assert.AreEqual(dmClass.MappedTableID, dbTable.TableID);
            Assert.IsNotNull(dmClass.MappedTable, "The class should be mapped to a table");
            Assert.IsNotNull(dbForeignKey.ParentDMClass());
            Assert.IsNull(dbForeignKey.RelatedDMClass());
            Assert.AreSame(dbTable, dmClass.MappedTable);
            Assert.AreSame(dbColumn.ColumnName, dmClass.Properties[0].MappedDBColumnName);
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship dmRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.IsNull(dmRelationship);
            Assert.AreEqual(1, errorMessages.Count);
            string message = errorMessages[0];
            string expectedMessage = string.Format
                ("on table '{0}' since it does not have a related or owner Class", dbTable.TableName);
            StringAssert.Contains(expectedMessage, message);
        }

        [Test]
        public void Test_Import_WithNoExistingRelationship_OneColumns_NoRelatedColumn_ShouldCreateRelationshipWithEmptyRelatedPropName()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = new DBForeignKey {ForeignKeyName = "ForeignKeyName"};

            DBColumn dbColumn = CreateColumnWithMappedProp("Some Table", "Some Column");
            dbColumn.GetMappedDMProperty().PropertyName = GetRandomString();
            DMClass dmClass = (DMClass) dbColumn.GetMappedDMProperty().Class;
            DBTable dbTable = (DBTable) dmClass.MappedTable;
            dbForeignKey.ParentTable = dbTable;
            dbForeignKey.RelatedTable = dbTable;

            dbForeignKey.AddColumnInfo(dbColumn, null);
            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dbForeignKey.ForeignKeyColumns.Count);
            AssertBusinessObjectContainsPropertyForColumn(dmClass, dbColumn);
            Assert.AreEqual(dmClass.MappedTableID, dbTable.TableID);
            Assert.IsNotNull(dmClass.MappedTable, "The class should be mapped to a table");
            Assert.AreSame(dbTable, dmClass.MappedTable);
            Assert.AreSame(dbColumn.ColumnName, dmClass.Properties[0].MappedDBColumnName);

            Assert.IsNotNull(dbForeignKey.ParentDMClass());
            Assert.IsNotNull(dbForeignKey.RelatedDMClass());
            Assert.IsNotNullOrEmpty(dbColumn.GetMappedPropertyName());
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship dmRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.IsNotNull(dmRelationship);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            var relProp = dmRelationship.RelationshipProperties.First();
            Assert.AreEqual(dbColumn.GetMappedPropertyName(), relProp.OwnerPropertyName);
            Assert.IsNullOrEmpty(relProp.RelatedPropertyName);
            Assert.AreEqual(3, errorMessages.Count);
            string message = errorMessages[0];
            string expectedMessage = string.Format
                ("since the column that relates to the foreign key column called '{0}' does not exist",
                 dbForeignKey.ForeignKeyColumns[0].ParentColumn.ColumnName);
            StringAssert.Contains(expectedMessage, message);
        }

        [Test]
        public void Test_Import_WithNoExistingRelationship_OneColumns_NoRelatedTable_ShouldCreateRel_AndHaveErrorList()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = new DBForeignKey {ForeignKeyName = "ForeignKeyName"};
            DBColumn dbColumn;
            DBTable dbTable = CreateDBTableWithOneColumn(out dbColumn);
            DMClass dmClass = CreateBusinessObjectWithPropertyForColumn(dbTable, dbColumn);
            dbForeignKey.ParentTable = dbTable;
            dbForeignKey.RelatedTable = dbTable;
            DBColumn relatedDBColumn = new DBColumn("relatedDBColumn");
            relatedDBColumn.SetMappedDMProperty(new DMProperty{PropertyName = GetRandomString()});
            dbColumn.GetMappedDMProperty().PropertyName = GetRandomString();
            dbForeignKey.AddColumnInfo(dbColumn, relatedDBColumn);
            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();

            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dbForeignKey.ForeignKeyColumns.Count);
            AssertBusinessObjectContainsPropertyForColumn(dmClass, dbColumn);
            Assert.AreEqual(dmClass.MappedTableID, dbTable.TableID);
            Assert.IsNotNull(dmClass.MappedTable, "The class should be mapped to a table");
            Assert.AreSame(dbTable, dmClass.MappedTable);
            Assert.AreEqual(dbColumn.ColumnName, dmClass.Properties[0].MappedDBColumnName);
            Assert.IsNotNull(dbForeignKey.ParentDMClass());
            Assert.IsNotNull(dbForeignKey.RelatedDMClass());
            Assert.IsNotNullOrEmpty(dbColumn.GetMappedPropertyName());
            Assert.IsNotNullOrEmpty(relatedDBColumn.GetMappedPropertyName());
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship dmRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.IsNotNull(dmRelationship);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            var relProp = dmRelationship.RelationshipProperties.First();
            Assert.AreEqual(dbColumn.GetMappedPropertyName(), relProp.OwnerPropertyName);
            Assert.AreEqual(relatedDBColumn.GetMappedPropertyName(), relProp.RelatedPropertyName);
            Assert.AreEqual(1, errorMessages.Count);
            string message = errorMessages[0];
            string expectedMessage = string.Format
                ("since the table for the related column '{0}' does not exist",
                 relatedDBColumn.ColumnName);
            StringAssert.Contains(expectedMessage, message);
        }

        [Test]
        public void TestAccept_Import_WithNoExistingRelationship_TwoColumns_ShouldSetUpRelWithBothRelProps()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = new DBForeignKey();
            DBColumn dbColumn;
            DBTable dbTable = CreateDBTableWithOneColumn(out dbColumn);
            dbForeignKey.ParentTable = dbTable;
            DBColumn dbColumnInfo2 = GetDBColumnInfo(dbTable);

            DBColumn relatedDBColumn;
            DBTable relatedTable = CreateDBTableWithOneColumn(out relatedDBColumn);
            dbForeignKey.RelatedTable = relatedTable;
            DBColumn relatedColumn2 = GetDBColumnInfo(relatedTable);

            DMClass dmClass = CreateBusinessObjectWithPropertyForTwoColumn(dbTable, dbColumn, dbColumnInfo2);
            DMClass relatedClass = CreateBusinessObjectWithPropertyForTwoColumn(relatedTable, relatedDBColumn,
                                                                                relatedColumn2);
            dbForeignKey.AddColumnInfo(dbColumn, relatedDBColumn);
            dbForeignKey.AddColumnInfo(dbColumnInfo2, relatedColumn2);


            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, dbForeignKey.ForeignKeyColumns.Count);
            AssertBusinessObjectContainsPropertyForColumn(dmClass, dbColumn);
            AssertBusinessObjectContainsPropertyForColumn(dmClass, dbColumnInfo2);
            Assert.IsNotNullOrEmpty(dbColumn.ColumnName);
            Assert.IsNotNullOrEmpty(dbColumnInfo2.ColumnName);
            Assert.IsNotNullOrEmpty(relatedDBColumn.ColumnName);
            Assert.IsNotNullOrEmpty(relatedColumn2.ColumnName);
            Assert.IsNotNull(dbForeignKey.ParentDMClass());
            Assert.IsNotNull(dbForeignKey.RelatedDMClass());
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship dmRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            GetDMSolutionPostLoadProcessor().ProcessRelationships(new[] {dmClass, relatedClass});
            //---------------Test Result -----------------------
            Assert.IsNotNull(dmRelationship);
            Assert.AreEqual(2, dmRelationship.RelationshipProperties.Count);
            DMProperty expectedBoProp = dmClass.Properties[0];
            Assert.IsTrue(dmRelationship.HasOwnerProperty(expectedBoProp));
            DMProperty expectedBoProp2 = dmClass.Properties[1];
            Assert.IsTrue(dmRelationship.HasOwnerProperty(expectedBoProp2));

            Assert.IsTrue(dmRelationship.HasRelatedProperty(relatedColumn2.GetMappedDMProperty()));
            Assert.AreSame(dmClass, dmRelationship.OwnerClass);
            Assert.AreSame(relatedClass, dmRelationship.RelatedClass);
            Assert.AreSame(relatedTable.GetMappedDMClass(), dmRelationship.RelatedClass);

            Assert.IsTrue(dmRelationship.Status.IsValid(), "Relationship is not valid." +
                                                           Environment.NewLine + "Reason: " +
                                                           dmRelationship.Status.IsValidMessage);
            Assert.IsTrue(dmRelationship.ReverseRelationship.Status.IsValid(), "Reverse Relationship is not valid." +
                                                                               Environment.NewLine + "Reason: " +
                                                                               dmRelationship.ReverseRelationship.Status
                                                                                   .IsValidMessage);
        }


        [Test]
        public void Test_Import_WithNoExistingRelationship_ColumnsNotInBo_ShouldCreateDMRelAndRelProps_AndRetErrors()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = new DBForeignKey {ForeignKeyName = "ForeignKeyName"};


            DBColumn dbColumn = new DBColumn("Some Column");
            DBColumn dbColumnInfo2 = new DBColumn("Some Column1");
            var dbTable = new DBTable();
            dbForeignKey.ParentTable = dbTable;
            var relatedTable = new DBTable();
            relatedTable.SetMappedDMClass (new DMClass());
            dbForeignKey.RelatedTable = relatedTable;
            DMClass dmClass = CreateBusinessObjectWithPropertyForTwoColumn(dbTable, dbColumn, dbColumnInfo2);

            DBColumn dbColumnNotInBo = new DBColumn("unmatched column");
            DBColumn relatedDBColumnNotInBo = new DBColumn("unmatched column related");
            dbForeignKey.AddColumnInfo(dbColumnNotInBo, relatedDBColumnNotInBo);
            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass.MappedTable);
            Assert.AreEqual(1, dbForeignKey.ForeignKeyColumns.Count);
            AssertBusinessObjectContainsPropertyForColumn(dmClass, dbColumn);
            AssertBusinessObjectContainsPropertyForColumn(dmClass, dbColumnInfo2);
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship dmRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.IsNotNull(dmRelationship);
            Assert.IsNotEmpty(dmRelationship.RelationshipProperties);
            Assert.AreEqual(2, errorMessages.Count);
            string message = errorMessages[0];
            string expectedMessage = string.Format
                ("since the foreign key column '{0}' is not mapped to a domain model class property",
                 dbColumnNotInBo.ColumnName);
            StringAssert.Contains(expectedMessage, message);
        }

        [Test]
        public void Test_Import_WithNoExistingRelationship_ShouldUseRelationshipNameGeneratorForRelationshipName()
        {
            //---------------Set up test pack-------------------
            DBColumn dbColumn = CreateColumnWithMappedProp("Some Table", "Some Column");
            DBColumn relatedDBColumn = CreateColumnWithMappedProp("Related Table", "relatedDBColumn");

            DBForeignKey dbForeignKey = new DBForeignKey {ForeignKeyName = "ForeignKeyName"};
            dbForeignKey.AddColumnInfo(dbColumn, relatedDBColumn);
            SetValidOwnerAndRelatedTable(dbForeignKey);
            IRelationshipNameGenerator relationshipNameGenerator = MockRepository.GenerateStub<IRelationshipNameGenerator>();
            string relationshipName = TestUtilsShared.GetRandomString();
            relationshipNameGenerator.Stub(
                generator => generator.CreateRelationshipName(Arg<DMRelationship>.Is.Anything)).Return(relationshipName);
            IDMSolution solution = MockRepository.GenerateMock<IDMSolution>();
            IDefClassFactory classFactory = new FMDefClassFactory(solution);
            DBToDMRelationshipImporter dbToDMRelationshipImporter =
                new DBToDMRelationshipImporter(relationshipNameGenerator, classFactory);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dbForeignKey.ParentDMClass());
            Assert.IsNotNull(dbForeignKey.RelatedDMClass());
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship dmRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.IsNotNull(dmRelationship);
            relationshipNameGenerator.AssertWasCalled(generator => generator.CreateRelationshipName(dmRelationship));
            Assert.AreEqual(relationshipName, dmRelationship.RelationshipName);
        }

        [Test]
        public void Test_Import_WithExistingRelationship_WhenHasRelationshipName_ShouldNotGenerateRelationshipName()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = CreateForeignKeyWithMappedRelationshipWithOneRelatedProp();
            DMRelationship dmRelationship = dbForeignKey.GetMappedDMRelationship();
            string relationshipName = dmRelationship.RelationshipName;

            IRelationshipNameGenerator relationshipNameGenerator =
                MockRepository.GenerateStub<IRelationshipNameGenerator>();
            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(relationshipName);
            Assert.AreEqual(1, dbForeignKey.ForeignKeyColumns.Count);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship returnedDMRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.AreSame(dmRelationship, returnedDMRelationship);
            relationshipNameGenerator.AssertWasNotCalled(generator => generator.CreateRelationshipName(dmRelationship));
            Assert.AreEqual(relationshipName, dmRelationship.RelationshipName);
        }

        [Test]
        public void Test_Import_WithExistingRelationship_WhenRelationshipNameIsNull_ShouldGenerateRelationshipName()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = CreateForeignKeyWithMappedRelationshipWithOneRelatedProp();
            DMRelationship dmRelationship = dbForeignKey.GetMappedDMRelationship();
            dmRelationship.RelationshipName = null;

            IRelationshipNameGenerator relationshipNameGenerator =
                MockRepository.GenerateStub<IRelationshipNameGenerator>();
            string relationshipName = TestUtilsShared.GetRandomString();
            relationshipNameGenerator.Stub(
                generator => generator.CreateRelationshipName(Arg<DMRelationship>.Is.Anything)).Return(relationshipName);
            IDMSolution solution = MockRepository.GenerateMock<IDMSolution>();
            IDefClassFactory classFactory = new FMDefClassFactory(solution);
            DBToDMRelationshipImporter dbToDMRelationshipImporter =
                new DBToDMRelationshipImporter(relationshipNameGenerator, classFactory);
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dbForeignKey.ForeignKeyColumns.Count);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship returnedDMRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.AreSame(dmRelationship, returnedDMRelationship);
            relationshipNameGenerator.AssertWasCalled(generator => generator.CreateRelationshipName(dmRelationship));
            Assert.AreEqual(relationshipName, dmRelationship.RelationshipName);
        }

/*
        [Test]
        public void Test_Import_WithNoExistingRelationship_ShouldUseRelationshipNameGeneratorForReverseRelationshipName()
        {
            //---------------Set up test pack-------------------
            DBColumn dbColumn = CreateColumnWithMappedProp("Some Table", "Some Column");
            DBColumn relatedDBColumn = CreateColumnWithMappedProp("Related Table", "relatedDBColumn");

            DBForeignKey dbForeignKey = new DBForeignKey { ForeignKeyName = "ForeignKeyName" };
            dbForeignKey.AddColumnInfo(dbColumn, relatedDBColumn);

            IRelationshipNameGenerator relationshipNameGenerator = MockRepository.GenerateStub<IRelationshipNameGenerator>();
            string relationshipName = TestUtilsShared.GetRandomString();
            relationshipNameGenerator.Stub(generator => generator.CreateRelationshipName(Arg<DMRelationship>.Is.Anything)).Return(relationshipName);
            IDMSolution solution = MockRepository.GenerateMock<IDMSolution>();
            IDefClassFactory classFactory = new FMDefClassFactory(solution);
            DBToDMRelationshipImporter dbToDMRelationshipImporter = new DBToDMRelationshipImporter(relationshipNameGenerator, classFactory);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship dmRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            relationshipNameGenerator.AssertWasCalled(generator => generator.CreateRelationshipName(dmRelationship.ReverseRelationship));
            Assert.AreEqual(relationshipName, dmRelationship.ReverseRelationship.RelationshipName);
        }*/
/*
        [Test]
        public void Test_Import_WithExistingRelationship_WhenHasReverseRelationshipName_ShouldNotGenerateReverseRelationshipName()
        {
            //---------------Set up test pack-------------------
            DBForeignKey dbForeignKey = CreateForeignKeyWithMappedRelationshipWithOneRelatedProp();
            DMRelationship dmRelationship = dbForeignKey.GetMappedDMRelationship();
            string relationshipName = dmRelationship.ReverseRelationship.RelationshipName;

            IRelationshipNameGenerator relationshipNameGenerator = MockRepository.GenerateStub<IRelationshipNameGenerator>();
            DBToDMRelationshipImporter dbToDMRelationshipImporter = CreateDbtoDMRelationshipImporter2();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(relationshipName);
            Assert.AreEqual(1, dbForeignKey.ForeignKeyColumns.Count);
            Assert.AreEqual(1, dmRelationship.RelationshipProperties.Count);
            //---------------Execute Test ----------------------
            IList<string> errorMessages;
            DMRelationship returnedDMRelationship = dbToDMRelationshipImporter.Import(dbForeignKey, out errorMessages);
            //---------------Test Result -----------------------
            Assert.AreSame(dmRelationship, returnedDMRelationship);
            relationshipNameGenerator.AssertWasNotCalled(generator => generator.CreateRelationshipName(dmRelationship.ReverseRelationship));
            Assert.AreEqual(relationshipName, dmRelationship.ReverseRelationship.RelationshipName);
        }*/

        private static DBColumn GetDBColumnInfo(DBTable dbTable)
        {
            DBColumn dbColumnInfo2 = new DBColumn("Some Column1");
            dbTable.Columns.Add(dbColumnInfo2);
            return dbColumnInfo2;
        }

        private static void AssertRelPropCreated(DBForeignKeyColumn fkColumn, DMRelationship relationship)
        {
            DMRelationshipProperty relProp =
                relationship.RelationshipProperties.FirstOrDefault(
                    property => property.OwnerPropertyName == fkColumn.GetParentPropertyName());
            Assert.IsNotNull(relProp);
            Assert.AreEqual(fkColumn.GetRelatedPropertyName(), relProp.RelatedPropertyName);
        }

        private static DBForeignKeyColumn GetDBForeignKeyColumn()
        {
            var dbForeignKeyColumn = TestUtilsDBForeignKeyColumn.GetTestFactory()
                .WithValueFor(column => column.RelatedColumn)
                .WithValueFor(column => column.ParentColumn)
                .WithValue(column => column.ForeignKey, null)
                .CreateValidBusinessObject();

            TestUtilsDMProperty.GetTestFactory()
                .WithValue(prop => prop.Class, null)
                .WithValue(prop => prop.DefaultPropRule, null)
                .WithValue(prop => prop.MappedDBColumn, dbForeignKeyColumn.ParentColumn)
                .CreateSavedBusinessObject();

            TestUtilsDMProperty.GetTestFactory()
                .WithValue(prop => prop.Class, null)
                .WithValue(prop => prop.DefaultPropRule, null)
                .WithValue(prop => prop.MappedDBColumn, dbForeignKeyColumn.RelatedColumn)
                .CreateSavedBusinessObject();

/*            var dbForeignKeyColumn = MockRepository.GenerateMock<FakeDBForeignKeyColumn>();
            dbForeignKeyColumn.Stub(column => column.GetParentPropertyName()).Return(GetRandomString());
            dbForeignKeyColumn.Stub(column => column.GetRelatedPropertyName()).Return(GetRandomString());
            return dbForeignKeyColumn;*/
/*            return new FakeDBForeignKeyColumn();*/
            return dbForeignKeyColumn;
        }

        private static string GetRandomString()
        {
            return TestUtilsShared.GetRandomString();
        }

        private static DBToDMRelationshipImporter GetDBtoDMRelationshipImporter()
        {
            IRelationshipNameGenerator relationshipNameGenerator =
                MockRepository.GenerateStub<IRelationshipNameGenerator>();
            IDMSolution solution = new DMSolution();
            IDefClassFactory classFactory = new FMDefClassFactory(solution);
            return new DBToDMRelationshipImporter(relationshipNameGenerator, classFactory);
        }

//        private static DBForeignKey CreateUpdatedForeignKeyWithMappedRelationship()
//        {
//            DBForeignKey foreignKey = CreateForeignKeyWithMappedRelationshipWithOneRelatedProp();
//            DBTable parentTable = foreignKey.ParentTable;
//            DMClass parentClass = parentTable.GetMappedDMClass();
//
//            DBColumn newFKColumn = new DBColumn("NewFKColumn");
//            parentTable.Columns.Add(newFKColumn);
//            DMProperty newFKProperty = new DMProperty { MappedDBColumn = newFKColumn };
//            parentClass.Properties.Add(newFKProperty);
//
//            foreignKey.ForeignKeyColumns[0].ParentColumn = newFKColumn;
//
//            return foreignKey;
//        }


        private static DBForeignKey CreateForeignKeyWithMappedRelationshipWithOneRelatedProp()
        {
            DBDatabase database = new DBDatabase();
            string relatedTableName = TestUtilsShared.GetRandomString();
            DBTable relatedTable = new DBTable(relatedTableName).ApproveForAdd();
            database.Tables.Add(relatedTable);
            string ownerTableName = TestUtilsShared.GetRandomString();
            DBTable ownerTable = new DBTable(ownerTableName).ApproveForAdd();
            database.Tables.Add(ownerTable);

            DBTableBuilder relatedTableBuilder = new DBTableBuilder(relatedTable);
            relatedTableBuilder.AddPrimaryKeyColumn().ApproveForAdd();

            DBTableBuilder ownerTableBuilder = new DBTableBuilder(ownerTable);
            DBForeignKey foreignKey =
                ownerTableBuilder.AddForeignKey(TestUtilsShared.GetRandomString(), relatedTable).ApproveForAdd();
            foreignKey.ForeignKeyColumns[0].ApproveForAdd().ParentColumn.ApproveForAdd();

            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            IDBTypeToDMPropertyTypeMapper dbTypeToDMPropertyTypeMapper =
                MockRepository.GenerateStub<IDBTypeToDMPropertyTypeMapper>();
            dbTypeToDMPropertyTypeMapper.Stub(t => t.GetDMPropertyType(null, null)).IgnoreArguments().Return(
                new DMPropertyType());
            IDBChangesImporter importer =
                new DBToDMImporterFactory(dbTypeToDMPropertyTypeMapper, new RelationshipNameGenerator()).
                    CreateDBChangesImporter();
            importer.ImportDBChanges(database, solution);

            return foreignKey;
        }

        private static DBToDMRelationshipImporter CreateDbtoDMRelationshipImporter2()
        {
            IRelationshipNameGenerator relationshipNameGenerator =
                MockRepository.GenerateStub<IRelationshipNameGenerator>();
            relationshipNameGenerator.Stub(t => t.CreateRelationshipName(null)).IgnoreArguments().Return(
                TestUtilsShared.GetRandomString());
            IDMSolution solution = MockRepository.GenerateMock<IDMSolution>();
            IDefClassFactory classFactory = new FMDefClassFactory(solution);
            return new DBToDMRelationshipImporter(relationshipNameGenerator, classFactory);
        }

        private static DBColumn CreateColumnWithMappedProp(string tableName, string columnName)
        {
            DBColumn dbColumn = new DBColumn(columnName);
            DBTable dbTable = new DBTable(tableName);
            dbTable.Columns.Add(dbColumn);
            CreateBusinessObjectWithPropertyForColumn(dbTable, dbColumn);
            return dbColumn;
        }

        private static DMClass CreateBusinessObjectWithPropertyForColumn(DBTable dbTable, DBColumn dbColumn)
        {
            DMSolution dmSolution = TestUtilsDMSolution.CreateSavedDMSolution();
            DBToDMClassImporter boImporter = new DBToDMClassImporter();
            DMClass dmClass = boImporter.Import(dmSolution, dbTable);
            IDBToDMPropertyUpdater dbToDMPropertyUpdater = MockRepository.GenerateStub<IDBToDMPropertyUpdater>();
            DBToDMPropertyImporter dbToDMPropertyImporter = new DBToDMPropertyImporter(dbToDMPropertyUpdater);
            dbToDMPropertyImporter.Import(dmClass, dbColumn);
            return dmClass;
        }

        private static DMClass CreateBusinessObjectWithPropertyForTwoColumn
            (DBTable dbTable, DBColumn dbColumn, DBColumn dbColumnInfo2)
        {
            DMClass dmClass = new DMClass {MappedTable = dbTable};
            dmClass.ClassNameBO = GetRandomString();
            dmClass.Assembly = new DMAssembly(GetRandomString());
            IDBToDMPropertyUpdater dbToDMPropertyUpdater =
                new DBToDMPropertyUpdater(MockRepository.GenerateMock<IDBTypeToDMPropertyTypeMapper>());
            DBToDMPropertyImporter dbToDMPropertyImporter = new DBToDMPropertyImporter(dbToDMPropertyUpdater);
            dbToDMPropertyImporter.Import(dmClass, dbColumn);
            dbToDMPropertyImporter.Import(dmClass, dbColumnInfo2);
            return dmClass;
        }

        private static void AssertBusinessObjectContainsPropertyForColumn(IDMClass dmClass, DBColumn info)
        {
            bool found = dmClass.Properties.Any(propertyInfo => propertyInfo.MappedDBColumnID == info.ColumnID);
            string message = string.Format
                ("The business object property for column {0} " + "was not found for business object {1}",
                 info.ColumnName, dmClass);
            Assert.IsTrue(found, message);
        }

//
//        private static DBTable GetTableInfoWithMappedBOInfo(out DMClass dmClass)
//        {
//            DBTable dbTable = new DBTable("RelatedTable");
//            DMSolution dmSolution = TestUtilsDMSolution.CreateSavedDMSolution();
//            DBToDMClassImporter dbToDMClassImporter = new DBToDMClassImporter();
//            dmClass = dbToDMClassImporter.Import(dmSolution, dbTable);
//            return dbTable;
//        }
//
//        private static DMSolution CreateDMSolution()
//        {
//            DMSolution dmSolution = new DMSolution("Some DMSolution");
//            DMAssembly dmAssembly = new DMAssembly(TestUtilsShared.GetRandomString());
//            dmSolution.Assemblies.Add(dmAssembly);
//            dmSolution.DefaultAssembly = dmAssembly;
//            return dmSolution;
//        }

        private static DBTable CreateDBTableWithOneColumn(out DBColumn dbColumn)
        {
            dbColumn = new DBColumn(TestUtilsShared.GetRandomString());
            DBTable dbTable = new DBTable(TestUtilsShared.GetRandomString());
            dbTable.Columns.Add(dbColumn);
            return dbTable;
        }
    }

    public class FakeClassFactory : FMDefClassFactory
    {
        public override IRelationshipDef CreateSingleRelationshipDef(string relationshipName, string relatedAssemblyName,
                                                                     string relatedClassName, IRelKeyDef relKeyDef,
                                                                     bool keepReferenceToRelatedObject,
                                                                     DeleteParentAction deleteParentAction,
                                                                     InsertParentAction insertParentAction,
                                                                     RelationshipType relationshipType)
        {
            return MockRepository.GenerateStub<DMRelationship>(ConstructForFakes.True);
        }
    }
    /// <summary>
    /// This fake is created for the purposes of being able to create a mock where
    /// these Extension Methods Can be Stubbed out
    ///        dbForeignKeyColumn.Stub(column => column.GetParentPropertyName()).Return(GetRandomString());
    ///        dbForeignKeyColumn.Stub(column => column.GetRelatedPropertyName()).Return(GetRandomString());
    /// </summary>
    public class FakeDBForeignKeyColumn : DBForeignKeyColumn
    {
        protected override IClassDef ConstructClassDef()
        {
            AllClassesAutoMapper.ClassDefCol = Habanero.BO.ClassDefinition.ClassDef.ClassDefs;
            return this.GetType().MapClass();
        }

        public virtual string GetParentPropertyName()
        {
            return TestUtilsShared.GetRandomString();
        }

        public virtual string GetRelatedPropertyName()
        {
            return TestUtilsShared.GetRandomString();
        }
    }

    /// <summary>
    /// This fake is created for the purposes of being able to create a mock where
    /// these Extension Methods Can be Stubbed out
    ///        ParentDMClass()
    ///        GetMappedDMRelationship()
    ///        RelatedDMClass()
    /// </summary>
    public class FakeDBForeignKey : DBForeignKey
    {
        protected override IClassDef ConstructClassDef()
        {
            AllClassesAutoMapper.ClassDefCol = Habanero.BO.ClassDefinition.ClassDef.ClassDefs;
            return this.GetType().MapClass();
        }
        /// <summary>
        /// The <see cref="DMClass"/> for this Foreign Key via its <see cref="DBForeignKey.ParentTable"/>
        /// </summary>
        public virtual IDMClass ParentDMClass()
        {
            return null;
        }
        /// <summary>
        /// The <see cref="DMClass"/> for this Foreign Key via its <see cref="DBForeignKey.RelatedTable"/>
        /// </summary>
        public virtual IDMClass RelatedDMClass()
        {
            return null;
        }
        public virtual DMRelationship GetMappedDMRelationship()
        {
            return null;
        }
    }
}